package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.testng.annotations.DataProvider;

public class ContactDataGenerator extends TestBase {

	static Random rnd = new Random();
		
	static int year = generateRandomNumberBetween(1930, 2013);
	static int day = generateRandomNumberBetween(01, 28);
	
	@DataProvider(name = "randomContacts")
	public static Iterator<Object[]> generateRandomContacts() {
		List<ContactObject> contacts = new ContactDataGenerator().generateRandomContactList(5);
		List<Object[]> list = new ArrayList<Object[]>();
		for (ContactObject contact : contacts) {
			list.add(new Object[]{contact});
		}
		return list.iterator();
	}
	@DataProvider(name = "contactsFromFile")
	public static Iterator<Object[]> loadContactsFromFile() throws IOException {
		List<Object[]> list = new ArrayList<Object[]>();
		//BufferedReader reader = new BufferedReader(new FileReader("contacts.dat"));
		BufferedReader reader = new BufferedReader(new FileReader(app.getProperty("contactFile")));
		String line = reader.readLine();
		while (line != null) {
			String[] parts = line.split("\t");
			ContactObject contact = new ContactObject()
				.setFirstname(parts[0])
				.setLastname(parts[1])
				.setAddress(parts[2])
				.setHome(parts[3])
				.setMobile(parts[4])
				.setWork(parts[5])
				.setEmail(parts[6])
				.setEmail2(parts[7])
				.setBday(parts[8])
				.setBmonth(parts[9])
				.setByear(parts[10])
				.setNew_group(parts[11])
				.setAddress2(parts[12])
				.setPhome2(parts[13]);
			list.add(new Object[]{contact});
			line = reader.readLine();
		}
		return list.iterator();
	}
	
	public static void main(String[] args) {
		if (args.length < 2) {
			System.out.println("Specify two parameters: file and count");
			return;
		}
		String file = args[0];
		int count = Integer.parseInt(args[1]);
		try {
			new ContactDataGenerator().generateDataToFile(file, count);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void generateDataToFile(String fileName, int count) throws IOException {
		List<ContactObject> contacts = generateRandomContactList(count);
		File file = new File(fileName);
		if (file.exists()) {
			//file.delete();
			System.out.println("File exists, stop generator");
			return;
		}
		FileWriter writer = new FileWriter(file);
		for (ContactObject contact : contacts) {
			writer.write(contact.firstname + "\t"
			+ contact.lastname + "\t"
			+ contact.address + "\t" 
			+ contact.home + "\t" 
			+ contact.mobile + "\t" 
			+ contact.work + "\t" 
			+ contact.email + "\t" 
			+ contact.email2 + "\t"
			+ contact.bday + "\t"
			+ contact.bmonth + "\t" 
			+ contact.byear + "\t" 
			+ contact.new_group + "\t" 
			+ contact.address2 + "\t" 
			+ contact.phome2 + "\t");
		}
		writer.close();
	}
	private List<ContactObject> generateRandomContactList(int count) {
		List<ContactObject> list = new ArrayList<ContactObject>();
		for (int i = 0; i < count; i++) {
			ContactObject contact = new ContactObject()
				.setFirstname("first"+rnd.nextInt() )
				.setLastname("last"+rnd.nextInt())
				.setAddress("address"+rnd.nextInt())
				.setHome("1234567"+rnd.nextInt())
				.setMobile("1234567"+rnd.nextInt())
				.setWork("1234567"+rnd.nextInt())
				.setEmail("as"+rnd.nextInt()+"@mail.ru")
				.setEmail2("buk"+rnd.nextInt()+"@mail.ru")
				.setBday(""+day)
				.setBmonth("May")
				.setByear(""+ year)
				.setNew_group("[none]")
				.setAddress2("address"+rnd.nextInt())
				.setPhome2("home"+rnd.nextInt());
			list.add(contact);
		}
		return list;
	}
}
